<template>
  <div class="app-container">
    <!-- 搜索区域 -->
    <div class="search">
      <el-form :inline="true" :model="form" class="demo-form-inline">
        <el-form-item label="合作商搜索:">
          <el-input v-model="area" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="searchOrderFn"
            >搜索</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <!-- 信息列表区域 -->
    <div class="result">
      <el-button class="btn" icon="el-icon-circle-plus" @click="addOrderFn"
        >新建</el-button
      >
      <!-- table表格区域 -->
      <el-table :data="tableData" style="width: 100%"
      :highlight-current-row="true"
       :cell-style="{padding: '5px'}">
        <el-table-column type="index" :index="indexFn" label="序号" width="80">
        </el-table-column>
        <el-table-column prop="name" label="合作商名称"> </el-table-column>
        <el-table-column prop="mobile" label="账号"> </el-table-column>
        <el-table-column prop="vmCount" label="设备数量"></el-table-column>
        <el-table-column prop="ratio" label="分成比例"></el-table-column>
        <el-table-column prop="contact" label="联系人"></el-table-column>
        <el-table-column prop="account" label="联系电话"></el-table-column>
        <el-table-column label="操作" width="280">
          <template v-slot="{ row }">
            <el-button type="text" size="medium" @click="open(row.id)">重置密码</el-button>
            <el-button type="text" size="medium" @click="lookInfoFn(row)">查看详情</el-button>
            <el-button type="text" size="medium" @click="editFn(row)">修改</el-button>
            <el-button type="text" style="color: red" size="medium" @click="delFn(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
        <div class="footbtn">
          <div class="page">
            <span> 共 {{totalCount}}条记录 第{{pageParams.pageIndex}}/{{ totalPage }}页</span>
          </div>
        <!-- 分页按钮 -->
          <el-row type="flex" justify="end">
          <el-pagination
          layout="prev, next"
          prev-text="上一页"
          :current-page.sync="pageParams.pageIndex"
          @current-change="changeFn"
          next-text="下一页"
          :total="totalCount">
        </el-pagination>
          </el-row>
        </div>
    </div>
    <!-- 新增弹层 -->
    <el-dialog :title="title" @close="closeFn" :visible.sync="showAddDialog" width="630px">
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">
        <el-form-item label="合作商名称:" prop="name">
          <el-input v-model="form.name" placeholder="请输入" style="width:396px" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="联系人:" prop="contact">
          <el-input v-model="form.contact" placeholder="请输入" style="width:396px" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="联系电话:" prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入" style="width:396px" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="分成比例(%):" prop="ratio">
           <el-input-number v-model="form.ratio"
           placeholder="请输入"
            style="width:396px"
            controls-position="right"
            :min="0" :max="100"></el-input-number>
        </el-form-item>
        <el-form-item v-if="!this.form.id" label="账号:" prop="account">
          <el-input v-model="form.account" placeholder="请输入" style="width:396px" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item v-if="!this.form.id" label="密码:" prop="account">
          <el-input v-model="form.password" placeholder="请输入" style="width:396px" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
        <el-row type="flex" justify="center">
          <el-button @click="closeFn = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
        </el-row>
    </el-dialog>
        <!-- 查看详情弹层 -->
    <el-dialog :visible.sync="showDetailDialog" title="合作商详情" width="630px">
      <el-form>
        <el-form-item label="合作商名称:">
          <el-row>
            金燕龙合作商
          </el-row>
        </el-form-item>
        <el-form-item label="联系人:">
          <el-row>
            张三
          </el-row>
        </el-form-item>
        <el-form-item label="联系电话:">
          <el-row>
            15555555555
          </el-row>
        </el-form-item>
        <el-form-item label="分成比例:">
          <el-row>
            15%
          </el-row>
        </el-form-item>
      </el-form>

    </el-dialog>
  </div>
</template>

<script>
import { getWithList, withSearch, addWith, resetPassword, delWith, editWith } from '@/api/place'
export default {
  data () {
    return {
      showDetailDialog: false, // 控制查看详情弹层显示
      showAddDialog: false, // 控制新增弹层显示
      pageParams: {
        pageIndex: 1,
        pageSize: 10
      },
      totalPage: 0, // 总页数
      totalCount: 0, // 总条数
      form: {
        ratio: 0, // 分成比例
        name: '', // 合作商名称
        contact: '', // 联系人
        mobile: '', // 联系人电话
        account: '', // 账号
        password: '' // 密码

      },
      area: '',
      tableData: [], // 区域列表数据
      rules: {
        name: [{ required: true, message: '请输入', trigger: 'blur' }],
        ratio: [{ required: true, message: '请输入', trigger: 'blur' }],
        contact: [{ required: true, message: '请输入', trigger: 'blur' }],
        mobile: [{ required: true, message: '请输入', trigger: 'blur' }],
        account: [{ required: true, message: '请输入', trigger: 'blur' }],
        password: [{ required: true, message: '请输入', trigger: 'blur' }]
      }
    }
  },
  created () {
    this.getWithList()
  },
  computed: {
    title () {
      return this.form.id ? '修改合作商' : '新增合作商'
    }
  },
  methods: {
    editFn (row) {
      this.showAddDialog = true
      this.form = row
      this.form.ratio = this.form.ratio.toString().slice(0, 2)
    },
    async delFn (id) {
      await delWith(id)
      this.$message.success('删除成功！')
      this.getWithList()
    },
    open (id) {
      this.$confirm('确定要重置合作商密码吗?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await resetPassword(id)
        this.$message.success('重置成功!')
        this.getWithList()
      })
    },
    // 关闭弹层函数
    closeFn () {
      this.$refs.form.resetFields()
      this.form = {
        ratio: null, // 分成比例
        name: '', // 合作商名称
        contact: '', // 联系人
        mobile: '', // 联系人电话
        account: '', // 账号
        password: '' // 密码

      }
    },
    // 提交
    async submit () {
      this.form.id
        ? await editWith({ ...this.form })
        : await addWith({ ...this.form })
      this.$message.success('新增成功')
      this.$message.success(`${this.form.id ? '修改' : '新增'}成功！`)
      this.showAddDialog = false
      this.getWithList()
    },
    changeFn () {
      this.getWithList()
    },
    // 获取区域列表
    async getWithList () {
      const res = await getWithList({ ...this.pageParams })
      this.tableData = res.data.currentPageRecords
      this.tableData.forEach(item => {
        item.ratio = `${item.ratio}%`
      })
      this.totalPage = res.data.totalPage
      this.totalCount = +res.data.totalCount
    },
    // 搜索
    async searchOrderFn () {
      const res = await withSearch({ ...this.pageParams, name: this.area })
      this.tableData = res.data.currentPageRecords
    },
    // 新增合作商
    addOrderFn () {
      this.showAddDialog = true
    },
    // 表格序号
    indexFn (index) {
      return index + this.pageParams.pageSize * (this.pageParams.pageIndex - 1) + 1
    },
    lookInfoFn () {
      this.showDetailDialog = true
    }
  }
}
</script>

<style lang="less" scoped>
.app-container {
  padding: 15px;
  .search {
    display: flex;
    height: 64px;
    align-items: center;
    padding-left: 17px;
    background-color: #fff;
    .demo-form-inline {
      margin-top: 21px;
    }
  }
  .result {
    margin-top: 20px;
    padding: 15px;
    background-color: #fff;
    .btn {
      background-color: #ff893a;
      color: #fff;
    }
     .table{
        padding: 20px 15px 19px 17px;
        background-color: #fff;
      }
      .footbtn{
        margin-top: 20px;
        display: flex;
        justify-content: space-between;
      }
  }
}
.primary1{
  margin-left: 40px !important;
  background-color: #ff722c;
  color: #fff;
}
.page{
    span{
    display: inline-block;
    font-size: 16px;
    color: #e5e2e5;
    min-width: 35.5px;
    height: 28px;
    line-height: 28px;
    vertical-align: top;
    box-sizing: border-box;
    }
}
/deep/ .btn-prev,/deep/.btn-next{
  span{
  display: inline-block;
  width: 70px;
  height: 32px;
  border-radius: 2px;
  background-color: #d5ddf8;
  }
}
/deep/.el-dialog {
  border-radius: 10px;
}
// /deep/ .el-table__cell{
//   line-height: 44px;
//   padding: 0;
//   margin: 0;
// }
</style>
